const loaderUtils = require('loader-utils');
const path = require('path');
const utils = require('./utils');
const pkgName = require('../package.json').name;

module.exports = function (source) {
  // var callback = this.async();
  const options = loaderUtils.getOptions(this);
  // console.log(options);
  const resource = this.resource;
  let entryArr = utils.getTempEntry();
  let isEntry = entryArr && entryArr.includes(resource); //是否入口文件
  // console.log('------js-loader--------------',  process.env.VUE_CLI_ENTRY_FILES, '----------', utils.getTempEntry());
  if (isEntry) {
    const config = utils.getConfig(undefined, options);
    // console.log('---------------', resource)
    let injectPreStr = utils.isDevelop() ? `import '${pkgName}/tmp/${config.cssFileName}.css'; ` : '';
    if (!options || options.isAutoJnjectComponent !== false) {
      injectPreStr += `import KissFont from '${pkgName}/src/index.vue';
        Vue.component('${config.componentTag}', KissFont);
        `;
    }
    //TODO 放后面无法注入组件，原因不明
    const injectEndStr = ``;
    // const injectEndStr = `Vue.component('kiss-font', KissFont); `;
    let res = injectPreStr + source + injectEndStr;
    // console.log(res)
    // callback(null, res);
    return res;
  }

  // callback(null, source);
  return source;
};